package com.lzb.permanentcalendar.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.permanentcalendar.BaseAc;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.adapter.AgeAdapter;
import com.lzb.permanentcalendar.adapter.SxSelAdapter;
import com.lzb.permanentcalendar.bean.SxBean;
import com.lzb.permanentcalendar.databinding.ActivityChineseZodiacAgeBinding;
import com.lzb.permanentcalendar.utils.DataListUtils;
import com.nlf.calendar.Solar;

import java.util.Date;
import java.util.List;

public class ChineseZodiacAgeActivity extends BaseAc<ActivityChineseZodiacAgeBinding> implements OnItemClickListener {

    private List<SxBean> sxList;
    private SxSelAdapter sxSelAdapter;
    private AgeAdapter ageAdapter;
    private int oldPosition;


    @Override
    protected int onCreate() {
        return R.layout.activity_chinese_zodiac_age;
    }

    @Override
    protected void initView() {
        mDataBinding.ivChineseZodiacAgeBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.rvSxSelList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        sxSelAdapter = new SxSelAdapter();
        mDataBinding.rvSxSelList.setAdapter(sxSelAdapter);
        sxSelAdapter.setOnItemClickListener(this);

        mDataBinding.rvAgeList.setLayoutManager(new LinearLayoutManager(mContext));
        ageAdapter = new AgeAdapter();
        mDataBinding.rvAgeList.setAdapter(ageAdapter);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        sxSelAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        sxSelAdapter.getItem(position).setSelected(true);
        sxSelAdapter.notifyDataSetChanged();
        Solar solar = Solar.fromDate(new Date());
        ageAdapter.setList(DataListUtils.getYearsByShengXiao(sxSelAdapter.getItem(position).getName(), 1900, solar.getYear()));
        mDataBinding.rvSxSelList.smoothScrollToPosition(position);
    }

    @Override
    protected void initData() {
        sxList = DataListUtils.getSxList();
        sxList.get(0).setSelected(true);
        sxSelAdapter.setList(sxList);
        Solar solar = Solar.fromDate(new Date());
        ageAdapter.setList(DataListUtils.getYearsByShengXiao(sxSelAdapter.getItem(oldPosition).getName(), 1900, solar.getYear()));
    }

}